﻿#region Copyright (c) 2011, thinktecture (http://www.thinktecture.com)

/*
   thinktecture.UIAnnotations was developed by Jörg Neumann.
   Copyright (c) 2011, thinktecture (http://www.thinktecture.com).
   All rights reserved, comes as-is and without any warranty. Use of this
   source file is governed by the license which is contained in LICENSE.TXT 
   in the distribution.
*/

#endregion

using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;

namespace Thinktecture.DataForm
{
    /// <summary>
    /// Represents the footer of the DataForm component.
    /// </summary>
    [TemplatePart(Name = "PART_ErrorImage", Type = typeof(Image))]
    [TemplatePart(Name="PART_DescriptonImage", Type=typeof(Image))]
    public class DataFormFooter : Control
    {

        #region Private Variables
        
        private Image _errorImage;
        private Image _DescriptionImage;

        #endregion

        #region Constructors
        
        static DataFormFooter()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DataFormFooter), new FrameworkPropertyMetadata(typeof(DataFormFooter)));
        }
        
        #endregion

        #region Overrides

        /// <summary>
        /// When overridden in a derived class, is invoked whenever application code or internal processes call System.Windows.FrameworkElement.ApplyTemplate().
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _errorImage = this.Template.FindName("PART_ErrorImage", this) as Image;
            _DescriptionImage = this.Template.FindName("PART_DescriptonImage", this) as Image;
        }
        
        #endregion

        #region Properties
        
        #region DescriptionImage

        public static readonly DependencyProperty DescriptionImageProperty =
            DependencyProperty.Register("DescriptionImage", typeof(ImageSource), typeof(DataFormFooter), new PropertyMetadata(null, OnDescriptionImagePropertyChanged));
        public ImageSource DescriptionImage { get { return (ImageSource)GetValue(DescriptionImageProperty); } set { SetValue(DescriptionImageProperty, value); } }

        private static void OnDescriptionImagePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ((DataFormFooter)sender).OnDescriptionImageChanged(sender, e);
        }

        private void OnDescriptionImageChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (_DescriptionImage == null) return;
            if (e.NewValue != null)
            {
                _DescriptionImage.Height = double.NaN;
                _DescriptionImage.Width = double.NaN;
                _DescriptionImage.LayoutTransform = null;
            }
            else
            {
                _DescriptionImage.Height = 16;
                _DescriptionImage.Width = 16;
                _errorImage.LayoutTransform = new ScaleTransform(1.5d, 1.5d);
            }
        }

        #endregion

        #region ErrorImage

        public static readonly DependencyProperty ErrorImageProperty =
            DependencyProperty.Register("ErrorImage", typeof(ImageSource), typeof(DataFormFooter), new PropertyMetadata(null, OnErrorImagePropertyChanged));
        public ImageSource ErrorImage { get { return (ImageSource)GetValue(ErrorImageProperty); } set { SetValue(ErrorImageProperty, value); } }

        private static void OnErrorImagePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ((DataFormFooter)sender).OnErrorImageChanged(sender, e);
        }

        private void OnErrorImageChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (_errorImage == null) return;
            if (e.NewValue != null)
            {
                _errorImage.Height=double.NaN;
                _errorImage.Width = double.NaN;
                _errorImage.LayoutTransform = null;
            }
            else
            {
                _errorImage.Height = 18;
                _errorImage.Width = 18;
                _errorImage.LayoutTransform = new ScaleTransform(1.5d,1.5d);
            }
        }
        
        #endregion

        #region ErrorBackground

        public static readonly DependencyProperty ErrorBackgroundProperty =
            DependencyProperty.Register("ErrorBackground", typeof(Brush), typeof(DataFormFooter));
        public Brush ErrorBackground { get { return (Brush)GetValue(ErrorBackgroundProperty); } set { SetValue(ErrorBackgroundProperty, value); } }

        #endregion

        #region ErrorForeground

        public static readonly DependencyProperty ErrorForegroundProperty =
            DependencyProperty.Register("ErrorForeground", typeof(Brush), typeof(DataFormFooter));
        public Brush ErrorForeground { get { return (Brush)GetValue(ErrorForegroundProperty); } set { SetValue(ErrorForegroundProperty, value); } }

        #endregion

        #region DescriptionText

        public static readonly DependencyProperty DescriptionTextProperty =
            DependencyProperty.Register("DescriptionText", typeof(string), typeof(DataFormFooter));
        public string DescriptionText { get { return (string)GetValue(DescriptionTextProperty); } set { SetValue(DescriptionTextProperty, value); } }

        #endregion

        #region ErrorText

        public static readonly DependencyProperty ErrorTextProperty =
            DependencyProperty.Register("ErrorText", typeof(string), typeof(DataFormFooter));
        public string ErrorText { get { return (string)GetValue(ErrorTextProperty); } set { SetValue(ErrorTextProperty, value); } }

        #endregion

        #region HasError

        public static readonly DependencyProperty HasErrorProperty =
            DependencyProperty.Register("HasError", typeof(bool), typeof(DataFormFooter), new UIPropertyMetadata(false));
        public bool HasError { get { return (bool)GetValue(HasErrorProperty); } set { SetValue(HasErrorProperty, value); } }

        #endregion

        #endregion

    }
}
